/*
 * 文件名：IOUtil.java
 * 版权：Copyright 2006-2011 AsiaInfo Tech. Co. Ltd. All Rights Reserved. 
 * 描述： IOUtil.java
 * 修改人：Administrator
 * 修改时间：2011-8-10
 * 修改内容：新增
 */
package com.fengxing.ams.pub.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import com.fengxing.ams.pub.UtilityException;

/**
 * 文件输入输出工具
 * 
 * @author Administrator
 * @version CTMS V100R001 2011-8-10
 * @since CTMS V100R001C01
 */
public final class IOUtil
{

    /**
     * 
     * InputStream 转换String
     * 
     * @param is
     *            InputStream
     * @return String
     */
    public static String ioToString(InputStream is)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int i = 0;
        try
        {
            byte[] byteLen = new byte[4096]; // 设置每次读取字节长度

            while ((i = is.read(byteLen)) != -1)
            {
                bos.write(byteLen, 0, i); // 写的大小由i决定。
            }
        }
        catch (IOException e)
        {
            throw new UtilityException(e);
        }
        finally
        {
            try
            {
                if (null != is)
                {
                    // IO关闭
                    is.close();
                }
            }
            catch (IOException e)
            {
                throw new UtilityException(e);
            }
        }

        try
        {
            return bos.toString("UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            throw new UtilityException(e);
        }
    }

    /**
     * 
     * InputStream 转换String
     * 
     * @param is
     *            InputStream
     * @return String
     */
    public static byte[] ioToByte(InputStream is)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int i = 0;
        try
        {
            byte[] byteLen = new byte[4096]; // 设置每次读取字节长度

            while ((i = is.read(byteLen)) != -1)
            {
                bos.write(byteLen, 0, i); // 写的大小由i决定。
            }
        }
        catch (IOException e)
        {
            throw new UtilityException(e);
        }
        finally
        {
            try
            {
                if (null != is)
                {
                    // IO关闭
                    is.close();
                }
            }
            catch (IOException e)
            {
                throw new UtilityException(e);
            }
        }

        return bos.toByteArray();
    }

    /**
     * 
     * 从异常信息thr中获取堆栈信息
     * 
     * @param thr
     *            异常信息
     * @return 返回该异常信息的堆栈
     */
    public static String getStrackFromThrowable(Throwable thr)
    {

        ByteArrayOutputStream baos = null;

        PrintStream strackStream = null;

        try
        {
            baos = new ByteArrayOutputStream();

            strackStream = new PrintStream(baos);

            thr.printStackTrace(strackStream);

            strackStream.flush();

            return baos.toString();

        }
        finally
        {
            IOUtil.closeIOStream(baos);
            IOUtil.closeIOStream(strackStream);
        }

    }

    /**
     * 
     * 关闭IO流
     * 
     * @param stream
     *            要关闭的IO流
     */
    public static void closeIOStream(InputStream stream)
    {
        if (null == stream)
        {
            return;
        }

        try
        {
            stream.close();
        }
        catch (IOException e)
        {
            return;
        }

    }

    /**
     * 
     * 关闭IO流
     * 
     * @param stream
     *            要关闭的IO流
     */
    public static void closeIOStream(OutputStream stream)
    {
        if (null == stream)
        {
            return;
        }

        try
        {
            stream.close();
        }
        catch (IOException e)
        {
            return;
        }
    }

}
